﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyBlog.IService;
using MyBlog.Model;
using System;
using System.Threading.Tasks;
using Utility;

namespace MyBlog.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]//使用JWT鉴权特性,控制器上面加,所有的Action都会带上
    public class BlogNewsController : ControllerBase
    {
        private readonly IBlogNewsService _iBlogNewsService;

        /// <summary>
        /// 博客控制器
        /// </summary>
        /// <param name="iBlogNewsService"></param>
        public BlogNewsController(IBlogNewsService iBlogNewsService)
        {
            this._iBlogNewsService = iBlogNewsService;
        }

        /// <summary>
        /// 获取博客
        /// </summary>
        /// <returns></returns>
        [HttpGet("BlogNews")]
        public async Task<ActionResult<ApiResult>> GetBlogNews()
        {
            var data = await _iBlogNewsService.QueryAsync();
            if (data == null)
            {
                return ApiResultHelper.Error("没有更多的文章");
            }
            return ApiResultHelper.Success(data);
        }

        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<ActionResult<ApiResult>> Create(string title, string content, int typeid)
        {
            //数据验证
            BlogNews blogNews = new()
            {
                BrowseCount = 0,
                Content = content,
                LikeCount = 0,
                Time = DateTime.Now,
                Title = title,
                TypeId = typeid,
                WriterId = 1
            };
            bool b = await _iBlogNewsService.CreateAsync(blogNews);
            if (!b) return ApiResultHelper.Error("添加失败,服务器发生错误");
            return ApiResultHelper.Success(blogNews);
        }

        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("Delete")]
        public async Task<ActionResult<ApiResult>> Delete(int id)
        {
            bool b = await _iBlogNewsService.DeleteAsync(id);
            if (!b)
                return ApiResultHelper.Error(msg: "删除失败"); 
            return ApiResultHelper.Success(b);
        }
         
        /// <summary>
        /// 编辑文章
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        [HttpPut("Edit")]
        public async Task<ActionResult<ApiResult>> Edit(int id, string title, string content, int typeid)
        {
            var blogNews = await _iBlogNewsService.FindAsync(id);
            if (blogNews == null) 
                return ApiResultHelper.Error("没有找到该文章");

            blogNews.Title = title;
            blogNews.Content = content; blogNews.TypeId = typeid;
            bool b = await _iBlogNewsService.EditAsync(blogNews); 
            if (!b) 
                return ApiResultHelper.Error("修改失败"); 
            
            return ApiResultHelper.Success(blogNews);
        }

    }
}
